<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="横幅标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="请输入横幅标题"></el-input>
      </el-form-item>
      <el-form-item label="跳转链接" prop="linkUrl">
        <el-input v-model="dataForm.linkUrl" placeholder="请输入跳转链接"></el-input>
      </el-form-item>
      <el-form-item label="展示图片" prop="picUrl">
        <single-image-upload prcScale="800x600" module="picUrl" v-model="dataForm.picUrl"></single-image-upload>
      </el-form-item>
      <el-form-item label="展示位置" size="mini" prop="position">
        <el-radio-group v-model="dataForm.position">
          <el-radio :label="1">首页</el-radio>
          <el-radio :label="2">内页</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="请输入活动内容"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否启用" size="mini" prop="enabled">
        <el-radio-group v-model="dataForm.enabled">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleImageUpload from '@/components/UploadImage/SingleImageUpload';
import { getBannerInfo, createBanner, updateBanner } from '@/api/banner/banner';

const defaultDataForm = {
  id: '',
  title: '',
  linkUrl: '',
  picUrl: '',
  position: '',
  content: '',
  startTime: '',
  endTime: '',
  enabled: '',
}

export default {
  components: {
    SingleImageUpload,
  },
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        title: [
          { required: true, message: '横幅标题不能为空', trigger: 'blur' }
        ],
        linkUrl: [
          { required: true, message: '跳转链接不能为空', trigger: 'blur' }
        ],
        picUrl: [
          { required: true, message: '展示图片不能为空', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '展示位置', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '活动内容不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        enabled: [
          { required: true, message: '是否启用不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getBannerInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.title = res.data.title,
              this.dataForm.linkUrl = res.data.linkUrl,
              this.dataForm.picUrl = res.data.picUrl,
              this.dataForm.position = res.data.position,
              this.dataForm.content = res.data.content,
              this.dataForm.startTime = res.data.startTime,
              this.dataForm.endTime = res.data.endTime,
              this.dataForm.enabled = res.data.enabled
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            title: this.dataForm.title,
            linkUrl: this.dataForm.linkUrl,
            picUrl: this.dataForm.picUrl,
            position: this.dataForm.position,
            content: this.dataForm.content,
            startTime: this.dataForm.startTime,
            endTime: this.dataForm.endTime,
            enabled: this.dataForm.enabled,
          }
          if (!this.dataForm.id) {
            // 创建
            createBanner(params).then(response => {

              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')

            })
          } else {
            // 更新
            updateBanner(params).then(response => {

              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')

            })
          }
        }
      })
    },
  }
}
</script>
